package com.suitsoft.ut.web.account;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.service.account.AccountManager;
import com.suitsoft.ut.web.CrudActionSupport;

/**
 * 修改密码
 *@author Suine
 */
@SuppressWarnings("serial")
@Results({@Result(name = CrudActionSupport.RELOAD ,location = "password.action" ,type = "redirect")})
public class PasswordAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private AccountManager accountManager;
	
	private String password;
	private User entity;
	private String oldPassword;


	@Override
	public String execute() throws Exception {
		LoginUser loginUser = UserHolder.getLoginUser();
		if(loginUser == null){
			addActionMessage("请重新登陆!");
			Struts2Utils.getResponse().sendRedirect("/ut/j_spring_security_logout");
			return SUCCESS;
		}
		entity = accountManager.getUser(loginUser.getId());
		return SUCCESS;
	}
	@LogAnnotation(log = "修改密码")
	public String save() throws Exception {
		LoginUser loginUser = UserHolder.getLoginUser();
		if(loginUser == null){
			addActionMessage("请重新登陆!");
			Struts2Utils.getResponse().sendRedirect("/ut/j_spring_security_logout");
			return SUCCESS;
		}
		if(!loginUser.getPassword().equals(oldPassword)){
			addActionMessage("原密码不正确！");
			return SUCCESS; 
		}
		entity = accountManager.getUser(loginUser.getId());
		entity.setPassword(password);
		accountManager.saveUser(entity);
		addActionMessage("修改成功！");
		Struts2Utils.getResponse().sendRedirect("/ut/j_spring_security_logout");
		return SUCCESS;
	}
	

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User getEntity() {
		return entity;
	}
	public void setEntity(User entity) {
		this.entity = entity;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
}

